#include "AbstractView.h"


AbstractView::AbstractView(QWidget *parent) : QGLWidget(parent)
{	
	timer = new QTimer(this);
	connect(timer, SIGNAL(timeout()), this, SLOT(updateGL()));
	timer->start(REFRESH_RATE);
}


// from Steven Longay, keeps the aspect ratio when resizing window
void AbstractView::resizeGL(int w, int h)
{
	// Switch to the propper matrix
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	
	// Set drawing to take up the entire window
	glViewport(0, 0, w, h);
	
	float ratio;
	if ( w > h ) // In this case the w/h ratio is > 1
	{
		ratio = (float)w/(float)h;
		glOrtho(-ratio, ratio, -1, 1, -2, 2);
	}
	else        // In this case the h/w ratio is > 1
	{
		ratio = (float)h/(float)w;
		glOrtho(-1, 1, -ratio, ratio, -2, 2);
	}
	
	//Switch back to modelview matrix
	glMatrixMode(GL_MODELVIEW);
}


QSize AbstractView::sizeHint() const
{
	return QSize(AbstractView::WIDGET_WIDTH, AbstractView::WIDGET_HEIGHT);
}


QSize AbstractView::minimumSizeHint() const
{
	return QSize(AbstractView::WIDGET_WIDTH, AbstractView::WIDGET_HEIGHT);
}



